package pers.zh.parking.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import pers.zh.parking.R;
import pers.zh.parking.data.UserSettings;
import pers.zh.parking.di.component.DaggerLoginComponent;
import pers.zh.parking.mvp.contract.LoginContract;
import pers.zh.parking.mvp.presenter.LoginPresenter;
import pers.zh.parking.utils.CommonTextWatcher;
import pers.zjc.commonlibs.util.StringUtils;
import pers.zjc.commonlibs.util.ToastUtils;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    TextView btnLogin;
    @BindView(R.id.tv_sign_up)
    TextView tvSignUp;
    @BindView(R.id.tilUserName)
    TextInputLayout tilUserName;
    @BindView(R.id.tilPassword)
    TextInputLayout tilPassword;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLoginComponent //如找不到该类,请编译一下项目
                             .builder().appComponent(appComponent).view(this).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initView();
        if (!UserSettings.getInstance().isLogin()) {
            return;
        }
        switchPage();
    }

    private void initView() {
        addTextWatcher(etUsername);
        addTextWatcher(etPassword);
        btnLogin.setOnClickListener(view -> {
            String account = etUsername.getText().toString();
            String password = etPassword.getText().toString();
            mPresenter.login(account, password);
        });
        tvSignUp.setOnClickListener(view -> {
            launchActivity(new Intent(LoginActivity.this, SignupActivity.class));
        });
    }

    private void addTextWatcher(EditText editText) {
        CommonTextWatcher watcher = new CommonTextWatcher(editText) {
            @Override
            public void afterChanged(String s) {
                switch (editText.getId()) {
                    case R.id.et_username:
                        if (StringUtils.isEmpty(s)) {
                            tilUserName.setError("账号不能为空");
                            tilUserName.setErrorEnabled(true);
                        } else {
                            tilUserName.setErrorEnabled(false);
                        }
                        break;
                    case R.id.et_password:
                        if (StringUtils.isEmpty(s)) {
                            tilPassword.setError("密码不能为空");
                            tilPassword.setErrorEnabled(true);
                        } else {
                            tilPassword.setErrorEnabled(false);
                        }
                        break;
                    default:
                        break;
                }
            }
        };
        editText.addTextChangedListener(watcher);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtils.showShort(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void loginSuccess() {
        switchPage();
    }

    private void switchPage() {
        String role = UserSettings.getInstance().getRole();
        if (StringUtils.equals(role, "1")) {
            goToMain();
        } else {
            goToAdmin();
        }
        finish();
    }

    private void goToAdmin() {
        launchActivity(new Intent(this, AdminActivity.class));
    }

    private void goToMain() {
        launchActivity(new Intent(this, MapActivity.class));
    }
}
